﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.events.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.effects.*;
    import Modules.bandits.*;
    import Display.*;
    import Classes.actions.*;
    import Modules.stats.experiments.*;
    import Engine.*;
    import com.adobe.utils.*;

    public class NPCManager implements IGameWorldUpdateObserver, IPlayerStateObserver {

        protected var m_world:GameWorld;
        protected var m_walkers:Array;
        protected var m_cars:Array;
        protected var m_ships:Array;
        protected var m_clouds:Array;
        protected var m_pickupPeeps:Dictionary;
        protected var m_deliveryPeeps:Dictionary;
        protected var m_builders:Dictionary;
        protected var m_carsMin:int;
        protected var m_shipsMin:int;
        protected var m_npcsNames:Array;
        protected var m_validNpcNames:Array;
        protected var m_carsNames:Array;
        protected var m_shipNames:Array;
        protected var m_maxVisibleWalkers:int;
        protected var m_maxTotalWalkers:int;
        protected var m_timer:Timer;
        protected var m_cacheResidential:Array;
        protected var m_cacheDecorations:Array;
        protected var m_cacheBusinesses:Array;
        protected var m_debugPathing:Boolean = false;
        protected var m_stragglerFraction:Number;
        protected var m_stragglerSpawnDelay:Number;
        protected var m_timeSinceLastWalkerUpdate:Number = 0;
        protected var m_timeSinceLastPeepTireOut:Number = 0;
        protected var m_timeSinceLastBusinessPeepSpawn:Number = 0;
        protected var m_peepTireOutDelay:Number;
        protected var m_peepTireOutFraction:Number;
        protected var m_businessPeepSpawnDelay:Number;
        protected var m_businessPeepSpawnFraction:Number;
        protected var m_businessPeepSpawnThreshold:Number;
        protected var m_spawnBusinessPeeps:Boolean = false;
        protected var m_housesOpen:Array;
        protected var m_housesClosed:Array;
        protected var m_timeSinceLastRandomDonutCop:Number = 0;
        protected var m_randomDonutCopDelay:Number = 0;

        public function NPCManager(_arg1:GameWorld){
            this.m_walkers = [];
            this.m_cars = [];
            this.m_ships = [];
            this.m_clouds = [];
            this.m_pickupPeeps = new Dictionary();
            this.m_deliveryPeeps = new Dictionary(true);
            this.m_builders = new Dictionary();
            super();
            this.m_world = _arg1;
            _arg1.addObserver(this);
            Global.player.addObserver(this);
        }
        public function initialize():void{
            this.m_walkers = [];
            this.m_cars = [];
            this.m_ships = [];
            this.m_clouds = [];
            this.m_housesOpen = [];
            this.m_housesClosed = [];
            this.m_deliveryPeeps = new Dictionary(true);
            this.m_builders = new Dictionary();
            this.m_pickupPeeps = new Dictionary();
            this.m_carsMin = Global.gameSettings.getInt("minCars");
            this.m_npcsNames = Global.gameSettings.getString("npcTypes", "walking_guy").split(",");
            this.m_carsNames = Global.gameSettings.getString("carTypes", "car").split(",");
            this.m_stragglerFraction = Global.gameSettings.getNumber("npcStragglerFraction", 0.1);
            this.m_stragglerSpawnDelay = Global.gameSettings.getNumber("npcStragglerSpawnDelay", 1);
            this.m_peepTireOutDelay = Global.gameSettings.getNumber("npcPeepTireOutDelay", 10);
            this.m_peepTireOutFraction = Global.gameSettings.getNumber("npcPeepTimeOutFraction", 0.1);
            this.m_businessPeepSpawnFraction = Global.gameSettings.getNumber("npcBusinessPeepSpawnFraction", 0.1);
            this.m_businessPeepSpawnThreshold = Global.gameSettings.getNumber("npcBusinessPeepSpawnThreshold", 7);
            this.m_businessPeepSpawnDelay = Global.gameSettings.getNumber("npcBusinessPeepSpawnDelay", 1);
            this.m_randomDonutCopDelay = Global.gameSettings.getNumber("CNBrandomDonutCopMinInterval", 60);
            this.m_shipsMin = Global.gameSettings.getInt("minShips");
            this.m_shipNames = Global.gameSettings.getString("shipTypes", "ship").split(",");
            this.m_maxVisibleWalkers = Global.gameSettings.getInt("maxVisibleWalkers", 75);
            this.m_maxTotalWalkers = Global.gameSettings.getInt("maxTotalWalkers", 350);
            var _local1:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_MAX_NUM_OF_WALKERS);
            if (_local1){
                this.m_maxVisibleWalkers = Math.min(this.m_maxVisibleWalkers, 50);
                this.m_maxTotalWalkers = Math.min(this.m_maxTotalWalkers, 100);
            };
            this.updateBuildingCaches();
            this.m_timer = new Timer(500);
            this.m_timer.addEventListener(TimerEvent.TIMER, this.onTimer);
        }
        public function cheatToggleNpcVisibility():void{
            var _local2:NPC;
            var _local1:Array = this.getAllWalkers();
            for each (_local2 in _local1) {
                _local2.alwaysCulled = true;
            };
        }
        public function cheatNpcRenderTest(_arg1:Boolean):void{
            var _local2:int;
            this.m_carsMin = 0;
            this.m_shipsMin = 0;
            this.m_npcsNames = Global.gameSettings.getString((_arg1) ? "cheatNpcBenderTypes" : "cheatNpcNormalTypes", "").split(",");
            while (_local2 < 30) {
                this.createPeepWalker(this.getRandomWalkerStartingPoint(), false);
                _local2++;
            };
        }
        public function cheatNpcEverybodyIdle():void{
            var _local2:NPC;
            var _local1:String = Global.gameSettings.getString("cheatNpcEverybodyAction", "");
            for each (_local2 in this.m_walkers) {
                _local2.getStateMachine().addState(new ActionPlayAnimation(_local2, _local1, 5));
            };
        }
        public function cleanUp():void{
            this.m_walkers = null;
            this.m_cars = null;
            this.m_ships = null;
            this.m_clouds = null;
            this.m_housesOpen = null;
            this.m_housesClosed = null;
            this.m_deliveryPeeps = null;
            this.m_builders = null;
            this.m_pickupPeeps = new Dictionary();
            this.m_cacheResidential = null;
            this.m_cacheDecorations = null;
            this.m_cacheBusinesses = null;
            this.m_timer.stop();
            this.m_timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
            this.m_timer = null;
        }
        public function onGameLoaded(_arg1:Object):void{
            var _local2:int;
            while (_local2 < this.m_carsMin) {
                this.makeRandomVehicle();
                _local2++;
            };
            this.updateBuildingCaches();
            this.initOpenHouses();
            this.createRandomStragglers(this.emptyCityStragglerCount);
            this.m_timer.start();
        }
        protected function get emptyCityStragglerCount():int{
            return (Math.min(this.m_businessPeepSpawnThreshold, Global.world.citySim.getPopulation()));
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function update(_arg1:Number):void{
            var _local2:int;
            if (_arg1){
                this.m_timeSinceLastWalkerUpdate = (this.m_timeSinceLastWalkerUpdate + _arg1);
                this.m_timeSinceLastPeepTireOut = (this.m_timeSinceLastPeepTireOut + _arg1);
                this.m_timeSinceLastBusinessPeepSpawn = (this.m_timeSinceLastBusinessPeepSpawn + _arg1);
                this.m_timeSinceLastRandomDonutCop = (this.m_timeSinceLastRandomDonutCop + _arg1);
            };
            if ((((this.m_timeSinceLastWalkerUpdate > this.m_stragglerSpawnDelay)) && (!(this.m_spawnBusinessPeeps)))){
                this.updateWalkers();
            };
            if (this.m_timeSinceLastPeepTireOut > this.m_peepTireOutDelay){
                this.tireOutPeeps(this.m_peepTireOutFraction);
                this.m_timeSinceLastPeepTireOut = 0;
            };
            if ((((this.m_timeSinceLastBusinessPeepSpawn > this.m_businessPeepSpawnDelay)) && (this.m_spawnBusinessPeeps))){
                this.createBusinessPeepWalkers(1);
                this.m_timeSinceLastBusinessPeepSpawn = 0;
            };
            if (this.m_timeSinceLastRandomDonutCop > this.m_randomDonutCopDelay){
                _local2 = (Math.random() * 20);
                if (_local2 < 2){
                    if ((((BanditUtil.getPoliceStationLevel() >= 2)) && (!(Global.world.citySim.banditManager.areStationsLocked())))){
                        BanditManager.spawnAmbientCopToDonutShop();
                    };
                };
                this.m_timeSinceLastRandomDonutCop = 0;
            };
        }
        public function updateWalkers():void{
            var _local2:Array;
            var _local3:Peep;
            var _local1:int = Math.max(0, (this.emptyCityStragglerCount - this.m_walkers.length));
            if (_local1 > 0){
                _local2 = this.createBusinessPeepWalkers(_local1);
                for each (_local3 in _local2) {
                    _local3.isStraggler = true;
                };
            };
            this.m_timeSinceLastWalkerUpdate = 0;
        }
        public function get walkerCount():int{
            return (this.m_walkers.length);
        }
        public function getVisibleWalkerCount():int{
            var _local2:NPC;
            var _local1:int;
            for each (_local2 in this.m_walkers) {
                if (_local2.isVisible()){
                    _local1++;
                };
            };
            return (_local1);
        }
        public function startSpawningBusinessPeeps():void{
            this.m_spawnBusinessPeeps = true;
        }
        public function stopSpawningBusinessPeeps():void{
            this.m_spawnBusinessPeeps = false;
        }
        public function updateBuildingCaches():void{
            this.m_cacheResidential = Global.world.getObjectsByClass(Residence);
            this.m_cacheDecorations = Global.world.getObjectsByClass(Decoration);
            this.m_cacheBusinesses = Global.world.getObjectsByClass(Business);
        }
        protected function initOpenHouses():void{
            var _local1:Residence;
            for each (_local1 in this.m_cacheResidential) {
                this.m_housesOpen.push(_local1);
            };
        }
        public function addNewResidence(_arg1:Residence):void{
            this.m_housesOpen.push(_arg1);
        }
        public function removeResidence(_arg1:Residence):void{
            var _local2:Array = (_arg1.hasOccupants()) ? this.m_housesOpen : this.m_housesClosed;
            var _local3:int = _local2.indexOf(_arg1);
            if (_local3 != -1){
                _local2.splice(_local3, 1);
            };
        }
        public function get cachedDecorations():Array{
            return (this.m_cacheDecorations);
        }
        public function get cachedResidences():Array{
            return (this.m_cacheResidential);
        }
        public function onBusinessClosed():void{
            var _local1:Array = Global.world.getObjectsByPredicate(this.predicateBusinessIsOpen);
            if (_local1.length == 0){
                this.stopSpawningBusinessPeeps();
                this.tireOutPeeps();
            };
        }
        protected function predicateBusinessIsOpen(_arg1:Object):Boolean{
            return ((((((_arg1 is Business)) && ((_arg1 as Business).isOpen()))) && (!((_arg1 as Business).isNeedingRoad))));
        }
        public function energyChanged(_arg1:Number):void{
        }
        public function levelChanged(_arg1:int):void{
        }
        public function commodityChanged(_arg1:int, _arg2:String):void{
        }
        public function xpChanged(_arg1:int):void{
        }
        public function goldChanged(_arg1:int):void{
        }
        public function cashChanged(_arg1:int):void{
        }
        public function get constructionWorkers():Dictionary{
            return (this.m_builders);
        }
        public function setConstructionWorkers(_arg1:MapResource, _arg2:Array):void{
            var _local3:int = ((this.m_builders[_arg1])!=null) ? (this.m_builders[_arg1] as Array).length : 0;
            if (_arg2.length < _local3){
                this.removeAllConstructionWorkers(_arg1);
                _local3 = 0;
            };
            this.addConstructionWorkers(_arg1, _arg2);
        }
        public function addConstructionWorkers(_arg1:MapResource, _arg2:Array):void{
            var _local3:Object;
            var _local4:NPC;
            if (this.m_builders[_arg1] == null){
                this.m_builders[_arg1] = [];
            };
            for each (_local3 in _arg2) {
                _local4 = new NPC("walking_guy", false);
                this.setBuilderAnimation(_local4, _arg1, _local3);
                _local4.setOuter(this.m_world);
                _local4.attach();
                this.m_builders[_arg1].push(_local4);
            };
        }
        public function removeAllConstructionWorkers(_arg1:MapResource):void{
            var _local2:NPC;
            if (((!(this.m_builders)) || ((this.m_builders[_arg1] == null)))){
                return;
            };
            for each (_local2 in this.m_builders[_arg1]) {
                _local2.detach();
                _local2.cleanUp();
            };
            delete this.m_builders[_arg1];
        }
        private function setBuilderAnimation(_arg1:NPC, _arg2:MapResource, _arg3:Object):void{
            var _local4:Vector3 = _arg2.getPosition();
            _arg2.getPosition().x = (_local4.x + _arg2.getConstructionNPCOffset().x);
            _local4.y = (_local4.y + _arg2.getConstructionNPCOffset().y);
            var _local5:Number = (_arg2.getItem().sizeX - 1);
            var _local6:Number = (_arg2.getItem().sizeY - 2);
            var _local7:Number = (_local4.x + 0.33);
            var _local8:Number = (_local4.y - 0.33);
            var _local9 = (Math.random() < (_local5 / (_local5 + _local6)));
            var _local10:int = ((this.m_builders[_arg2])!=null) ? this.m_builders[_arg2].length : 0;
            var _local11:int = _local10;
            _arg1.setDirection((_local9) ? Constants.DIRECTION_NW : Constants.DIRECTION_NE);
            if (_local9){
                if (_local11 > _local5){
                    _local11 = Math.floor((Math.random() * _local5));
                };
                _local7 = ((_local4.x + _local11) + 1);
                _arg1.setPosition(_local7, _local8);
            } else {
                if (_local11 > _local6){
                    _local11 = Math.floor((Math.random() * _local6));
                };
                _local8 = ((_local4.y + _local11) + 1);
                _arg1.setPosition(_local7, _local8);
            };
            var _local12 = "hammer";
            if (_arg3.isTempWorker){
                _arg1.getStateMachine().addState(new ActionPlayAnimation(_arg1, _local12, 2), true);
                _arg1.getStateMachine().addState(new ActionPlayAnimation(_arg1, "cloud", 0.5), true);
                _arg1.getStateMachine().addState(new ActionDie(_arg1), true);
            } else {
                _arg1.getStateMachine().addState(new ActionPlayAnimation(_arg1, "cloud", 0.5));
                _arg1.getStateMachine().addState(new ActionPlayAnimation(_arg1, _local12, Number.POSITIVE_INFINITY), true);
            };
        }
        public function getPickupNPC(_arg1:String):NPC{
            return ((this.m_pickupPeeps[_arg1]) ? this.m_pickupPeeps[_arg1] : null);
        }
        public function createPickupNPC(_arg1:String, _arg2:Boolean):void{
            var _local3:Class = (_arg2) ? DummyNPC : NPC;
            var _local4:NPC = new _local3(_arg1, false);
            _local4.setOuter(this.m_world);
            _local4.attach();
            this.m_pickupPeeps[_arg1] = _local4;
            UI.popupLock();
            Global.world.citySim.pickupManager.addPickupAction(_local4);
        }
        public function removePickupNPC(_arg1:String):void{
            if (this.m_pickupPeeps[_arg1]){
                this.m_pickupPeeps[_arg1].detach();
                this.m_pickupPeeps[_arg1].cleanUp();
                delete this.m_pickupPeeps[_arg1];
                UI.popupUnlock();
            };
        }
        protected function createRandomStragglers(_arg1:int):Array{
            var _local3:Peep;
            var _local4:Road;
            var _local2:Array = this.createBusinessPeepWalkers(_arg1);
            for each (_local3 in _local2) {
                _local3.isStraggler = true;
                _local4 = Global.world.citySim.roadManager.findRandomRoad();
                if (_local4 != null){
                    _local3.setPosition(_local4.getPosition().x, _local4.getPosition().y);
                };
            };
            return (_local2);
        }
        public function createBusinessPeepWalkers(_arg1:int):Array{
            var _local5:int;
            var _local6:Residence;
            var _local7:Boolean;
            var _local2:int = Math.max(0, (this.m_maxTotalWalkers - this.m_walkers.length));
            _arg1 = Math.min(_arg1, _local2);
            var _local3:Array = [];
            if ((((this.m_housesOpen == null)) || ((this.m_housesOpen.length == 0)))){
                return (_local3);
            };
            var _local4:int;
            while (_local4 < _arg1) {
                if (this.m_housesOpen.length > 0){
                    _local5 = MathUtil.randomIndex(this.m_housesOpen);
                    _local6 = this.m_housesOpen[_local5];
                    _local7 = _local6.takeResident();
                    _local3.push(this.createPeepWalker(_local6, false));
                    if (_local7){
                        this.m_housesOpen.splice(_local5, 1);
                        this.m_housesClosed.push(_local6);
                    };
                };
                _local4++;
            };
            return (_local3);
        }
        public function removeBusinessPeepWalkers(_arg1:int):void{
            var _local4:int;
            var _local5:Residence;
            var _local2:Array = this.findAllPeeps();
            var _local3:int;
            while (_local3 < _arg1) {
                this.removeNpc(MathUtil.randomElement(_local2));
                if (((!((this.m_housesClosed == null))) && ((this.m_housesClosed.length > 0)))){
                    _local4 = MathUtil.randomIndex(this.m_housesClosed);
                    _local5 = this.m_housesClosed[_local4];
                    _local5.returnResident();
                    this.m_housesClosed.splice(_local4, 1);
                    this.m_housesOpen.push(_local5);
                };
                _local3++;
            };
        }
        public function onResidentReturnHome(_arg1:Residence):void{
            if ((((this.m_housesClosed == null)) || ((this.m_housesClosed.length == 0)))){
                return;
            };
            var _local2:int = this.m_housesClosed.indexOf(_arg1);
            if (_local2 != -1){
                _arg1.returnResident();
                this.m_housesClosed.splice(_local2, 1);
                this.m_housesOpen.push(_arg1);
            };
        }
        public function getRandomReturnHome():Residence{
            if (((!((this.m_housesClosed == null))) && ((this.m_housesClosed.length > 0)))){
                return (MathUtil.randomElement(this.m_housesClosed));
            };
            return (null);
        }
        public function tireOutPeeps(_arg1:int=-1):void{
            var _local4:Peep;
            var _local2:Array = this.findAllPeeps();
            _arg1 = ((_arg1 == -1)) ? _local2.length : _arg1;
            var _local3:int;
            while (_local3 < _arg1) {
                _local4 = MathUtil.randomElement(_local2);
                if (!_local4.isFranchiseFreebie){
                    _local4.isTired = true;
                    _local4.getStateMachine().removeAllStates();
                };
                _local3++;
            };
        }
        public function createPeepWalker(_arg1:IPeepWalker, _arg2:Boolean):Peep{
            var peep:* = null;
            var source:* = _arg1;
            var freebie:* = _arg2;
            var position:* = source.getHotspot();
            var resource:* = source.getMapResource();
            peep = (this.addWalker(this.getNpcName(resource), true, position, Peep) as Peep);
            peep.animation = "cheer";
            peep.getStateMachine().addActions(new ActionAnimationEffect(peep, EffectType.FLASH), new ActionFn(peep, function ():void{
                peep.animation = "static";
            }));
            if ((resource is Residence)){
                peep.setHome((resource as Residence));
            };
            if (((!(freebie)) && ((this.m_walkers.length > this.m_maxVisibleWalkers)))){
                if (this.getVisibleWalkerCount() > this.m_maxVisibleWalkers){
                    peep.alwaysCulled = true;
                };
            };
            return (peep);
        }
        public function set showNPC(_arg1:Boolean):void{
            var _local2:int;
            if (_arg1){
                this.m_maxVisibleWalkers = Global.gameSettings.getInt("maxVisibleWalkers", 75);
                this.m_maxTotalWalkers = Global.gameSettings.getInt("maxTotalWalkers", 350);
                _local2 = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_MAX_NUM_OF_WALKERS);
                if (_local2){
                    this.m_maxVisibleWalkers = Math.min(this.m_maxVisibleWalkers, 50);
                    this.m_maxTotalWalkers = Math.min(this.m_maxTotalWalkers, 100);
                };
            } else {
                this.m_maxVisibleWalkers = 0;
                this.m_maxTotalWalkers = 0;
            };
        }
        public function createPeepWalkerAtPosition(_arg1:Vector3, _arg2:Boolean, _arg3:String=null):Peep{
            var peep:* = null;
            var source:* = _arg1;
            var freebie:* = _arg2;
            var name = _arg3;
            var peepName:* = ((name)!=null) ? name : this.getNpcName(null);
            peep = (this.addWalker(peepName, true, source, Peep) as Peep);
            peep.animation = "cheer";
            peep.getStateMachine().addActions(new ActionAnimationEffect(peep, EffectType.FLASH), new ActionFn(peep, function ():void{
                peep.animation = "static";
            }));
            return (peep);
        }
        public function findAllPeeps():Array{
            return (this.m_walkers.filter(function (_arg1:NPC, ... _args):Boolean{
                return ((_arg1 is Peep));
            }));
        }
        public function findAllPeepsByPredicate(_arg1:Function):Array{
            var predicate:* = _arg1;
            return (this.m_walkers.filter(function (_arg1:NPC, ... _args):Boolean{
                var _local3:* = false;
                if ((_arg1 is Peep)){
                    _local3 = predicate((_arg1 as Peep));
                };
                return (_local3);
            }));
        }
        public function getAllNpcNames():Array{
            return (this.m_npcsNames);
        }
        protected function getNpcName(_arg1:MapResource, _arg2:int=-1):String{
            var _local3:Array = ((_arg1)!=null) ? _arg1.npcNames : null;
            if ((((_local3 == null)) || ((_local3.length == 0)))){
                _local3 = this.m_npcsNames;
            };
            if (_arg2 >= 0){
                _arg2 = (_arg2 % _local3.length);
                return (_local3[_arg2]);
            };
            return (MathUtil.randomElement(_local3));
        }
        public function createWalker(_arg1:MapResource, _arg2:Boolean):NPC{
            var _local3:Vector3 = _arg1.getHotspot();
            return (this.addWalker(this.getNpcName(_arg1), _arg2, _local3, NPC));
        }
        public function createWalkerAtPosition(_arg1:Vector3, _arg2:Boolean):NPC{
            return (this.addWalker(this.getNpcName(null), _arg2, _arg1, NPC));
        }
        public function createWalkerByNameAtPosition(_arg1:String, _arg2:Vector3, _arg3:Boolean):NPC{
            return (this.addWalker(_arg1, _arg3, _arg2, NPC));
        }
        public function createMayorAtPosition(_arg1:Vector3):NPC{
            return (this.addWalker("NPC_mayor", false, _arg1, NPC));
        }
        public function createVehicleByName(_arg1:Road, _arg2:String, _arg3:Boolean):Vehicle{
            return (this.addVehicleByName(_arg2, _arg1, _arg3));
        }
        public function createShipByNameAtPosition(_arg1:String, _arg2:Vector3, _arg3:Array, _arg4:Boolean, _arg5:Number=-1):SeaVehicle{
            return (this.addShipByName(_arg1, _arg2, _arg3, _arg4, _arg5));
        }
        public function createCloudByNameAtPosition(_arg1:String, _arg2:Vector3, _arg3:Array, _arg4:Boolean):Cloud{
            return (this.addCloudByName(_arg1, _arg2, _arg3, _arg4));
        }
        public function addVehicle(_arg1:Vehicle):void{
            _arg1.setOuter(this.m_world);
            _arg1.attach();
            this.m_cars.push(_arg1);
        }
        public function removeWalker(_arg1:NPC):void{
            this.removeTrackedNpc(_arg1, this.m_walkers);
        }
        public function removeVehicle(_arg1:Vehicle):void{
            this.removeTrackedNpc(_arg1, this.m_cars);
        }
        public function removeShip(_arg1:SeaVehicle):void{
            this.removeTrackedNpc(_arg1, this.m_ships);
        }
        public function removeCloud(_arg1:Cloud):void{
            this.removeTrackedNpc(_arg1, this.m_clouds);
        }
        private function removeTrackedNpc(_arg1:NPC, _arg2:Array):void{
            if (_arg2.indexOf(_arg1) == -1){
                return;
            };
            ArrayUtil.removeValueFromArray(_arg2, _arg1);
            _arg1.detach();
            _arg1.cleanUp();
        }
        public function removeNpc(_arg1:NPC):void{
            if ((_arg1 is Vehicle)){
                this.removeVehicle((_arg1 as Vehicle));
            } else {
                if ((_arg1 is SeaVehicle)){
                    this.removeShip((_arg1 as SeaVehicle));
                } else {
                    this.removeWalker(_arg1);
                };
            };
        }
        public function isNpcTracked(_arg1:NPC):Boolean{
            if ((_arg1 is Vehicle)){
                return (!((this.m_cars.indexOf(_arg1) == -1)));
            };
            if ((_arg1 is SeaVehicle)){
                return (!((this.m_ships.indexOf(_arg1) == -1)));
            };
            return (!((this.m_walkers.indexOf(_arg1) == -1)));
        }
        public function getFreeAgentWalkers(_arg1:MapResource):Array{
            var _local4:NPC;
            var _local5:ActionNavigate;
            var _local2:RoadGraph = this.m_world.citySim.roadManager.getConnectedRoadGraph(_arg1);
            if (_local2 == null){
                return ([]);
            };
            var _local3:Array = [];
            for each (_local4 in this.m_walkers) {
                if (_local4.isFreeAgent){
                    _local5 = (_local4.getStateMachine().getState() as ActionNavigate);
                    if (((!((_local5 == null))) && ((_local5.graph == _local2)))){
                        _local3.push(_local4);
                    };
                };
            };
            return (_local3);
        }
        public function createMoverPeeps(_arg1:Residence, _arg2:int):Array{
            var peep:* = null;
            var empty:* = false;
            var house:* = _arg1;
            var count:* = _arg2;
            var results:* = [];
            var i:* = 0;
            peep = null;
            i = 0;
            while (i < count) {
                empty = house.takeResident();
                peep = this.createPeepWalker(house, false);
                peep.setHome(house);
                peep.getStateMachine().removeAllStates();
                peep.getStateMachine().addActions(new ActionNavigateBeeline(peep, house.getHotspot(), null), new ActionPause(peep, 1.5), new ActionFn(peep, function ():void{
                    peep.animation = "static";
                }), new ActionEnableFreedom(peep, true, true));
                results.push(peep);
                i = (i + 1);
            };
            return (results);
        }
        public function createBusinessPeepsWaiting(_arg1:IMerchant, _arg2:int):Array{
            var _local3:Array = [];
            var _local4:int;
            var _local5:Peep;
            var _local6:MapResource = _arg1.getMapResource();
            if (Global.isVisiting()){
                _local4 = 0;
                while (_local4 < _arg2) {
                    _local5 = this.createPeepWalker(_arg1, true);
                    _local5.spawnSource = Peep.SOURCE_TOURBUS;
                    _local5.getStateMachine().removeAllStates();
                    _local5.getStateMachine().addActions(new ActionNavigate(_local5, _local6, null).setPathType(RoadManager.PATH_TO_FRONT_ENTRANCE), new ActionBusinessPacing(_local5, _local6));
                    _local3.push(_local5);
                    _local4++;
                };
            } else {
                _local4 = 0;
                while (_local4 < _arg2) {
                    _local5 = this.createPeepWalker(_arg1, true);
                    _local5.spawnSource = Peep.SOURCE_TOURBUS;
                    _local5.getStateMachine().removeAllStates();
                    _local5.getStateMachine().addActions(new ActionNavigate(_local5, _local6, null).setPathType(RoadManager.PATH_TO_FRONT_ENTRANCE), new ActionPause(_local5, 3), new ActionBusinessEnter(_local5, _arg1));
                    _local3.push(_local5);
                    _local4++;
                };
            };
            return (_local3);
        }
        public function createFranchiseFreebiePeeps(_arg1:Business, _arg2:int):Array{
            var _local3:Array = new Array();
            var _local4:Peep;
            var _local5:int;
            while (_local5 < _arg2) {
                _local4 = this.createPeepWalker(_arg1, true);
                _local4.isFranchiseFreebie = true;
                _local4.getStateMachine().removeAllStates();
                _local4.getStateMachine().addActions(new ActionNavigate(_local4, _arg1, null).setPathType(RoadManager.PATH_TO_FRONT_ENTRANCE), new ActionBusinessPacing(_local4, _arg1));
                _local3.push(_local4);
                _local5++;
            };
            return (_local3);
        }
        function getAllWalkers():Array{
            return (ArrayUtil.copyArray(this.m_walkers));
        }
        protected function getRandomWalkerStartingPoint():Residence{
            var _local2:Array;
            var _local3:Residence;
            if (((this.m_debugPathing) && (Global.world.citySim.roadManager.isShowingOverlay))){
                _local2 = this.m_world.getObjectsByClass(Residence);
                _local3 = ((_local2.length > 1)) ? _local2[1] : null;
                return (_local3);
            };
            var _local1:Residence = MathUtil.randomElement(this.m_world.getObjectsByClass(Residence));
            return (_local1);
        }
        public function getRandomWalkerOrigin(_arg1:MapResource, _arg2:uint=0):MapResource{
            var _local3:RoadGraph = this.m_world.citySim.roadManager.getConnectedRoadGraph(_arg1);
            if (_local3 == null){
                return (_arg1);
            };
            var _local4:MapResource = _local3.findRandomResource(Residence, _arg1, _arg2);
            if (_local4 != null){
                return (_local4);
            };
            var _local5:MapResource = _local3.findRandomResource(null, _arg1, _arg2);
            return (((_local5)!=null) ? _local5 : _arg1);
        }
        public function getRandomWalkerTarget(_arg1:MapResource, _arg2:uint=0):MapResource{
            var _local3:RoadGraph = this.m_world.citySim.roadManager.getConnectedRoadGraph(_arg1);
            if (_local3 == null){
                return (_arg1);
            };
            var _local4:MapResource = _local3.findRandomResource(null, _arg1, _arg2);
            return (((_local4)!=null) ? _local4 : _arg1);
        }
        private function addWalker(_arg1:String, _arg2:Boolean, _arg3:Vector3, _arg4:Class):NPC{
            var _local5:NPC = new _arg4(_arg1, _arg2);
            if (_arg3 != null){
                _local5.setPosition(_arg3.x, _arg3.y);
            };
            _local5.setOuter(this.m_world);
            _local5.attach();
            this.m_walkers.push(_local5);
            return (_local5);
        }
        private function addVehicleByName(_arg1:String, _arg2:Road, _arg3:Boolean):Vehicle{
            var _local4:Road;
            var _local5:Vehicle;
            if (!_arg3){
                _local4 = Global.world.citySim.roadManager.findRandomRoadNearEdges(_arg2);
                if ((((((((((_arg2 == null)) || ((_local4 == null)))) || (_arg2.isBeingMoved()))) || (_local4.isBeingMoved()))) || ((_arg2 == _local4)))){
                    return (null);
                };
                _local5 = new Vehicle(_arg1, false);
                _local5.setPosition(_arg2.getHotspot().x, _arg2.getHotspot().y);
                _local5.getStateMachine().addActions(new ActionNavigate(_local5, _local4, _arg2), new ActionDie(_local5));
            } else {
                _local5.getStateMachine().addState(new ActionNavigateRandom(_local5));
            };
            _local5.setOuter(this.m_world);
            _local5.attach();
            this.m_cars.push(_local5);
            return (_local5);
        }
        private function addShipByName(_arg1:String, _arg2:Vector3, _arg3:Array, _arg4:Boolean, _arg5:Number=-1):SeaVehicle{
            var _local6:SeaVehicle;
            var _local7:Vector3;
            var _local8:Vector3;
            if (!_arg4){
                _local6 = new SeaVehicle(_arg1, false, _arg5);
                _local6.setPosition(_arg2.x, _arg2.y);
                _local7 = _arg2;
                for each (_local8 in _arg3) {
                    _local6.getStateMachine().addActions(new ActionNavigateBeeline(_local6, _local8, _local7));
                    _local7 = _local8;
                };
                _local6.getStateMachine().addActions(new ActionDie(_local6));
            } else {
                _local6.getStateMachine().addState(new ActionNavigateRandom(_local6));
            };
            _local6.setOuter(this.m_world);
            _local6.attach();
            this.m_ships.push(_local6);
            return (_local6);
        }
        public function addFlyerByName(_arg1:Vehicle, _arg2:Vector3, _arg3:Array):Vehicle{
            _arg1.setPosition(_arg2.x, _arg2.y);
            _arg1.getStateMachine().addActionsArray(_arg3);
            _arg1.setOuter(this.m_world);
            _arg1.attach();
            this.m_clouds.push(_arg1);
            return (_arg1);
        }
        private function addCloudByName(_arg1:String, _arg2:Vector3, _arg3:Array, _arg4:Boolean):Cloud{
            var _local5:Cloud;
            var _local6:Vector3;
            var _local7:Vector3;
            if (!_arg4){
                _local5 = new Cloud(_arg1, false);
                _local5.setPosition(_arg2.x, _arg2.y);
                _local6 = _arg2;
                for each (_local7 in _arg3) {
                    _local5.getStateMachine().addActions(new ActionNavigateBeeline(_local5, _local7, _local6));
                    _local6 = _local7;
                };
                _local5.getStateMachine().addActions(new ActionDie(_local5));
            } else {
                _local5.getStateMachine().addState(new ActionNavigateRandom(_local5));
            };
            _local5.setOuter(this.m_world);
            _local5.attach();
            this.m_clouds.push(_local5);
            return (_local5);
        }
        private function onTimer(_arg1:TimerEvent):void{
            if (this.m_debugPathing){
                Global.world.citySim.roadManager.showOverlay(true);
            };
            if (this.m_cars.length < this.m_carsMin){
                this.makeRandomVehicle();
            };
        }
        public function makeRandomVehicle():void{
            var _local1:Road = Global.world.citySim.roadManager.findRandomRoadNearEdges();
            if (_local1 == null){
                return;
            };
            this.createVehicleByName(_local1, MathUtil.randomElement(this.m_carsNames), false);
        }

    }
}//package Classes.sim 
